package activity.base.base;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wangbinbin.mooccomponentapp_181103.R;

import view.fragment.Home.HomeFragment;
import view.fragment.Home.MessageFragment;
import view.fragment.Home.MineFragment;

/**
 * Created by wangbinbin on 2018/11/3.
 */

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "HomeActivity";
    private TextView mHomeButton;
    private TextView mMessageButton;
    private TextView mMineButton;
    private FragmentManager fm;
    private Fragment mHomeFragment;
    private Fragment mMessageFragment;
    private Fragment mMineFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);
        initView();

        mHomeFragment = new HomeFragment();
        fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        //这个容器不用fbc了，直接replace即可,第一次装填的时候使用replace，之后都用hide和show
        fragmentTransaction.replace(R.id.fragment_content_layout, mHomeFragment);
        fragmentTransaction.commit();
    }

    private void initView() {
        mHomeButton = (TextView) findViewById(R.id.activity_home_button);
        mHomeButton.setOnClickListener(this);
        mMessageButton = (TextView) findViewById(R.id.activity_message_button);
        mMessageButton.setOnClickListener(this);
        mMineButton = (TextView) findViewById(R.id.activity_mine_button);
        mMineButton.setOnClickListener(this);
    }

    private void hideFragment(Fragment fragment, FragmentTransaction ft) {
        if (fragment != null) {
            ft.hide(fragment);
        }
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.activity_home_button:
                hideFragment(mMessageFragment, fragmentTransaction);
                hideFragment(mMineFragment, fragmentTransaction);
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.fragment_content_layout, mHomeFragment);
                } else {
                    fragmentTransaction.show(mHomeFragment);
                }
                break;
            case R.id.activity_message_button:
                hideFragment(mHomeFragment, fragmentTransaction);
                hideFragment(mMineFragment, fragmentTransaction);
                if (mMessageFragment == null) {
                    mMessageFragment = new MessageFragment();
                    fragmentTransaction.add(R.id.fragment_content_layout, mMessageFragment);
                } else {
                    fragmentTransaction.show(mMessageFragment);
                }
                break;
            case R.id.activity_mine_button:
                hideFragment(mHomeFragment, fragmentTransaction);
                hideFragment(mMessageFragment, fragmentTransaction);
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.fragment_content_layout, mMineFragment);
                } else {
                    fragmentTransaction.show(mMineFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }
}
